/// <reference path="libs/jquery-2.0.2.js" />

//
// ScoreBoard
//
// Method add: should have Player object passed with name and score properties.
//
// Method display: returns top 5 score board as an ordered list.
//
// Method reset: erases top scores from local storage.
//

var scoreBoard = (function () {
    function add(player) {
        localStorage[player.score] = player.name;
    };

    function display() {
        var scoreBoard = JSON.parse(JSON.stringify(localStorage));
        var topFiveScoresCounter = 0;
        var ol = document.createElement("ol");
        var scoresArr = [];

        for (score in scoreBoard) {
            scoresArr.push({
                name: scoreBoard[score],
                score: score
            });
        };

        for (var i = scoresArr.length - 1; i >= 0; i--) {
            var li = document.createElement("li");
            li.innerHTML = "Name: " + scoresArr[i].name + ", Score: " + scoresArr[i].score;
            ol.appendChild(li);
            topFiveScoresCounter++;

            if (topFiveScoresCounter == 5) {
                break;
            };
        };

        return ol;
    };

    function resetHighscores() {
        localStorage.clear();
    };

    return {
        add: add,
        display: display,
        reset: resetHighscores
    };
})();